package com.gitee.wsl.compose.chart.legend

//package io.github.koalaplot.core.legend

import androidx.compose.foundation.layout.LayoutScopeMarker
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.layout.ParentDataModifier
import androidx.compose.ui.unit.Density
import com.gitee.wsl.compose.chart.SelectChangedListener
import com.gitee.wsl.compose.chart.SelectCheck

object Legend

typealias LegendSlate = @Composable (selectCheck: SelectCheck,listener: SelectChangedListener?) -> Unit

val Legend.defaultLegend :LegendSlate get() = {selected,listener-> }

typealias LegendSymbolSlate = @Composable LegendScope.(index:Int,selected:Boolean) -> Unit

typealias LegendLabelSlate = @Composable LegendScope.(index:Int,selected:Boolean,listener: SelectChangedListener?) -> Unit

typealias LegendValueSlate = @Composable LegendScope.(index:Int,selected:Boolean,listener: SelectChangedListener?) -> Unit


val Legend.defaultLegendSymbolSlate:LegendSymbolSlate get() = { index,selected -> }

val Legend.defaultLegendLabelSlate:LegendLabelSlate get() = { index,selected,listener -> }

/**
 * Scope for the children of a [Column] or [GridLegend].
 */
@LayoutScopeMarker
 interface LegendScope {
    /**
     * Align the element vertically within the cell.
     */
     fun Modifier.align(alignment: Alignment.Vertical): Modifier = this.then(
        VerticalAlignModifier(vertical = alignment)
    )

    /**
     * Align the element horizontally within the cell.
     */
     fun Modifier.align(alignment: Alignment.Horizontal): Modifier = this.then(
        HorizontalAlignModifier(horizontal = alignment)
    )
}

internal val legendScopeInstance = object : LegendScope {}

private class VerticalAlignModifier(val vertical: Alignment.Vertical) : ParentDataModifier {
    override fun Density.modifyParentData(parentData: Any?): Any =
        ((parentData as? LegendParentData) ?: LegendParentData()).also {
            it.verticalAlignment = vertical
        }
}

private class HorizontalAlignModifier(val horizontal: Alignment.Horizontal) : ParentDataModifier {
    override fun Density.modifyParentData(parentData: Any?): Any =
        ((parentData as? LegendParentData) ?: LegendParentData()).also {
            it.horizontalAlignment = horizontal
        }
}
